package org.smart4j.chapter2.service;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javafx.scene.control.ButtonBar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.chapter2.helper.DatabaseHelper;
import org.smart4j.chapter2.model.Customer;
import org.smart4j.chapter2.util.PropsUtil;

/**
 * 提供客户数据服务
 */
public class CustomerService {
    /*private static final String DRIVER;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;
    */
    private static final Logger LOGGER= LoggerFactory.getLogger(PropsUtil.class);
    /*
    static {
        Properties conf= PropsUtil.LoadProps("config.properties");
        DRIVER=conf.getProperty("jdbc.driver");
        URL=conf.getProperty("jdbc.url");
        USERNAME=conf.getProperty("jdbc.username");
        PASSWORD=conf.getProperty("jdbc.password");
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            LOGGER.error("can not load jdbc driver", e);
        }
    }*/
    /**
     * 获取客户列表
     */
    public List<Customer> getCustomerList() {
        //TODO
        //Connection conn=null;
        //conn= DatabaseHelper.getConnection();
        //try {
            //List<Customer> customerList=new ArrayList<Customer>();
            String sql="SELECT * FROM customer";
            //conn= DriverManager.getConnection(URL,USERNAME,PASSWORD);
            return DatabaseHelper.queryEntityList(Customer.class,sql);
            /*PreparedStatement stmt=conn.prepareStatement(sql);
            ResultSet rs=stmt.executeQuery();
            while(rs.next()) {
                Customer customer=new Customer();
                customer.setId(rs.getInt("id"));
                customer.setName(rs.getString("name"));
                customer.setContact(rs.getString("contact"));
                customer.setTelephone(rs.getString("telephone"));
                customer.setEmail(rs.getString("email"));
                customer.setRemark(rs.getString("remark"));
                customerList.add(customer);
            }*/
            //return customerList;
        //} catch (SQLException e) {
        //    LOGGER.error("execute sql failure",e);
        //} finally {
            /*if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e){
                    LOGGER.error("close connection faiure",e);
                }
            }*/
            //DatabaseHelper.closeConnection();
        //}
    }
    /**
     * 获取客户
     */
    public Customer getCustomer(long id) {
        //TODO
        return null;
    }
    /**
     * 创建客户
     */
    public boolean createCustomer(Map<String, Object> fieldMap) {
        //TODO
        return DatabaseHelper.insertEntity(Customer.class,fieldMap);
    }
    /**
     * 更新客户
     */
    public boolean updateCustomer(long id,Map<String,Object> fieldMap) {
        //TODO
        return DatabaseHelper.updateEntity(Customer.class,id,fieldMap);
    }

    /**
     *删除客户
     */
    public boolean deleteCustomer(long id) {
        //TODO
        return DatabaseHelper.deleteEntity(Customer.class,id);
    }
}
